package org.example.erat.service.parser;

import org.apache.poi.ss.usermodel.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelParser implements FileParser {

    @Override
    public List<Student> parse(InputStream inputStream) {
        List<Student> students = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) continue; // 跳过表头
                String id = row.getCell(0).getStringCellValue().trim();
                String name = row.getCell(1).getStringCellValue().trim();
                String major = row.getCell(2).getStringCellValue().trim();
                students.add(new Student(id, name, major));
            }
        } catch (Exception e) {
            throw new RuntimeException("Excel解析失败：" + e.getMessage(), e);
        }
        return students;
    }
}
